﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.Linq;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Data.SqlClient;

namespace WindowsFormsApplication1
{
    public partial class MonitorForm : Form
    {
        sensor_type s_type;
        LoginForm loginwindow;

        public MonitorForm()
        {
            InitializeComponent();
            this.skinEngine1.SkinFile = "MP10.ssk";

            ReadCrystalReportDB();
        }

        public MonitorForm(LoginForm lf)
        {
            InitializeComponent();
            this.skinEngine1.SkinFile = "MP10.ssk";

            loginwindow = lf;

            ReadCrystalReportDB();
        }

        private void ReadCrystalReportDB()
        {
            if (!CrystalReport.crystalReportConnectDB.configXMLdata())
            {
                MessageBox.Show("不能打开水晶报表！", "报表读取失败！", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }

        private void updateTreeView1()
        {
            TreeNode rootNode = new TreeNode("监控箱区域");
            treeView1.Nodes.Add(rootNode);

            var queryMonitor =
                from mon in Monitor_DB.instance.db.tb_monitor_device
                select mon;

            foreach (var item in queryMonitor)
            {
                bool added = false;
                foreach (TreeNode node in rootNode.Nodes)
                {
                    if (node.Text == item.Zone)
                    {
                        added = true;
                    }
                }

                if (!added)
                {
                    TreeNode newNode = new TreeNode(item.Zone, 2, 0);

                    rootNode.Nodes.Add(newNode);
                }
            }
            
        }

        private void refreshTreeView1()
        {
            treeView1.Nodes.Clear();
            updateTreeView1();
        }

        private void 刷新ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            refreshTreeView1();
        }
        

        private void MonitorForm_Load(object sender, EventArgs e)
        {
            panel1.Visible = false;
            
            this.listView2.Items[0].Tag = sensor_type.door;
            this.listView2.Items[1].Tag = sensor_type.tempture;
            this.listView2.Items[2].Tag = sensor_type.humidity;
            this.listView2.Items[3].Tag = sensor_type.shake;
            this.listView2.Items[4].Tag = sensor_type.voltage;

            this.toolStripButton10.Enabled = true;
            this.toolStripButton11.Enabled = false;

            disablemenuitem();

            updateTreeView1();
        }


        public ListView Get_MonitorForm_ListView1()
        {
            return this.listView1;
        }

        private void toolStripButton2_Click(object sender, EventArgs e)
        {
            if (this.listView2.View == View.List)
            {
                this.listView2.View = View.LargeIcon;
            }
            else
            {
                this.listView2.View = View.List;
            }
        }

        private void toolStripButton3_Click(object sender, EventArgs e)
        {
            Alarm.alarmDetect.instance.show_High();
        }

        private void toolStripButton4_Click(object sender, EventArgs e)
        {
            Alarm.alarmDetect.instance.show_Mid();
        }

        private void toolStripButton9_Click(object sender, EventArgs e)
        {
            Alarm.alarmDetect.instance.show_Low();
        }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            this.listView1.View = View.LargeIcon;
        }

        private void toolStripButton5_Click(object sender, EventArgs e)
        {
            this.listView1.View = View.SmallIcon;
        }

        private void toolStripButton6_Click(object sender, EventArgs e)
        {
            this.listView1.View = View.Tile;
        }

        private void toolStripButton7_Click(object sender, EventArgs e)
        {
            this.listView1.View = View.List;
        }

        private void toolStripButton8_Click(object sender, EventArgs e)
        {
            this.listView1.View = View.Details;
        }

        private alarm_type updateMonitorDevAlarmType(Int32 MonitorDev_Id)
        {
            alarm_type rc = alarm_type.normal;

            try
            {

                var queryTemp =
                from tem in Monitor_DB.instance.db.tb_tempture
                where tem.MonitorDevice_Id == MonitorDev_Id
                select tem;

                foreach (var item in queryTemp)
                {
                    // Alarm Type
                    alarm_type temp = Alarm.alarmDetect.instance.DetermineAlarmType(sensor_type.tempture, Convert.ToDouble(item.Value), Convert.ToDouble(item.Threshold),
                        Convert.ToDouble(item.Threshold_Two), Convert.ToDouble(item.Threshold_Three));

                    if (temp < rc)
                    {
                        //告警升级
                        rc = temp;
                    }
                }

               var queryHum =
               from hum in Monitor_DB.instance.db.tb_humidity
               where hum.MonitorDevice_Id == MonitorDev_Id
               select hum;

                foreach (var item in queryTemp)
                {
                    // Alarm Type
                    alarm_type temp = Alarm.alarmDetect.instance.DetermineAlarmType(sensor_type.humidity, Convert.ToDouble(item.Value), Convert.ToDouble(item.Threshold),
                        Convert.ToDouble(item.Threshold_Two), Convert.ToDouble(item.Threshold_Three));

                    if (temp < rc)
                    {
                        //告警升级
                        rc = temp;
                    }
                }

               var queryShake =
               from shake in Monitor_DB.instance.db.tb_shake
               where shake.MonitorDevice_Id == MonitorDev_Id
               select shake;

               foreach (var item in queryShake)
                {
                    // Alarm Type
                    alarm_type temp = Alarm.alarmDetect.instance.DetermineAlarmType(sensor_type.shake, Convert.ToDouble(item.Value), Convert.ToDouble(item.Threshold),
                        Convert.ToDouble(item.Threshold_Two), Convert.ToDouble(item.Threshold_Three));

                    if (temp < rc)
                    {
                        //告警升级
                        rc = temp;
                    }
                }

               var queryVolt =
               from volt in Monitor_DB.instance.db.tb_voltage
               where volt.MonitorDevice_Id == MonitorDev_Id
               select volt;

                foreach (var item in queryVolt)
                {
                    // Alarm Type
                    alarm_type temp = Alarm.alarmDetect.instance.DetermineAlarmType(sensor_type.voltage, Convert.ToDouble(item.Value), Convert.ToDouble(item.Threshold),
                        Convert.ToDouble(item.Threshold_Two), Convert.ToDouble(item.Threshold_Three));

                    if (temp < rc)
                    {
                        //告警升级
                        rc = temp;
                    }
                }

                return rc;
            }
            catch (Exception Ex)
            {
                MessageBox.Show(Ex.Message, "错误！", MessageBoxButtons.OK, MessageBoxIcon.Error);
                Monitor_DB.instance.Refresh_DB();

                return rc;
            }

        }

        private void treeView1_DoubleClick(object sender, EventArgs e)
        {
            try
            {
                Monitor_DB.instance.db.Refresh(RefreshMode.KeepChanges, Monitor_DB.instance.db.tb_monitor_device);

                if (this.treeView1.SelectedNode == null)
                {
                    return;
                }

                this.listView1.Items.Clear();

                string t = this.treeView1.SelectedNode.Text;

                var queryMonitor =
                    from mon in Monitor_DB.instance.db.tb_monitor_device
                    where mon.Zone == t
                    select mon;

                this.listView1.Columns.Clear();

                this.listView1.Columns.Add("      区域");
                this.listView1.Columns[0].Width = 150;

                this.listView1.Columns.Add("监控箱序列号");
                this.listView1.Columns[1].Width = 100;

                this.listView1.Columns.Add("监控箱设备号");
                this.listView1.Columns[2].Width = 100;

                this.listView1.Columns.Add("状态");
                this.listView1.Columns.Add("告警");
                this.listView1.Columns.Add("在用");
                this.listView1.Tag = this.treeView1.SelectedNode;

                progressBar1.Visible = true;
                progressBar1.Value = 0;
                progressBar1.Minimum = 0;
                progressBar1.Maximum = queryMonitor.Count();
                progressBar1.Step = 1;
                foreach (var item in queryMonitor)
                {
                    ListViewItem i = new ListViewItem(item.Zone);
                    i.SubItems.Add(item.ID.ToString());
                    i.SubItems[1].Text = item.Name;

                    i.SubItems.Add(item.Name);

                    if (item.Status != null)
                    {
                        i.SubItems.Add(item.Status.ToString());
                    }

                    // AlarmType
                    alarm_type alarmT = updateMonitorDevAlarmType(item.ID);
                    switch (alarmT)
                    {
                        case alarm_type.normal:
                            i.SubItems.Add("正常");
                            i.BackColor = Color.White;
                            break;

                        case alarm_type.mini:
                            i.SubItems.Add("低");
                            i.BackColor = Color.LightGreen;
                            break;

                        case alarm_type.med:
                            i.SubItems.Add("中");
                            i.BackColor = Color.Orange;
                            break;

                        case alarm_type.high:
                            i.SubItems.Add("高");
                            i.BackColor = Color.Red;
                            break;

                        default:
                            break;
                    }

                    if (item.InUse != null)
                    {
                        i.SubItems.Add(item.InUse.ToString());
                    }

                    this.listView1.Items.Add(i);
                    progressBar1.PerformStep();
                }
            }

            catch (Exception Ex)
            {
                MessageBox.Show(Ex.Message, "错误！", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

            progressBar1.Visible = false;
        }

        private void treeView1_Click(object sender, EventArgs e)
        {
            
        }


        private void clickListView2()
        {
            try
            {
                this.listView1.Items.Clear();

                sensor_type t = sensor_type.door;
                if (this.listView2.SelectedItems.Count != 0)
                {
                    t = (sensor_type)this.listView2.SelectedItems[0].Tag;
                }

                switch (t)
                {
                    case sensor_type.tempture:
                        try
                        {
                            Monitor_DB.instance.db.Refresh(System.Data.Linq.RefreshMode.KeepChanges, Monitor_DB.instance.db.tb_tempture);
                            s_type = sensor_type.tempture;
                            var queryTemp =
                                from tem in Monitor_DB.instance.db.tb_tempture
                                join mon in Monitor_DB.instance.db.tb_monitor_device on tem.MonitorDevice_Id equals mon.ID
                                select new { VAL1 = tem, VAL2 = mon };

                            this.listView1.Columns.Clear();

                            this.listView1.Columns.Add("监控箱设备号");
                            this.listView1.Columns[0].Width = 160;
                            this.listView1.Columns.Add("传感器序列号");
                            this.listView1.Columns[1].Width = 100;
                            this.listView1.Columns.Add("名称");
                            this.listView1.Columns[2].Width = 130;
                            this.listView1.Columns.Add("区域");
                            this.listView1.Columns.Add("状态");
                            this.listView1.Columns.Add("阀值1");
                            this.listView1.Columns.Add("阀值2");
                            this.listView1.Columns.Add("阀值3");
                            this.listView1.Columns.Add("温度");
                            this.listView1.Columns.Add("在用");
                            this.listView1.Tag = Monitor_DB.instance.db.tb_tempture;

                            this.数据上报ToolStripMenuItem.Enabled = true;
                            this.智能门锁ToolStripMenuItem2.Enabled = false;

                            progressBar1.Visible = true;
                            progressBar1.Value = 0;
                            progressBar1.Minimum = 0;
                            progressBar1.Maximum = queryTemp.Count();
                            progressBar1.Step = 1;
                            foreach (var item in queryTemp)
                            {
                                ListViewItem i = new ListViewItem(item.VAL2.ID.ToString());
                                i.SubItems[0].Name = item.VAL2.ID.ToString();
                                i.SubItems[0].Text = item.VAL2.Name.ToString();

                                i.ImageIndex = 6;
                                i.SubItems.Add(item.VAL1.ID.ToString());
                                i.SubItems.Add(item.VAL1.Name);
                                i.SubItems.Add(item.VAL2.Zone);

                                if (item.VAL1.Status != null)
                                {
                                    i.SubItems.Add(item.VAL1.Status.ToString());
                                }
                                else
                                {
                                    i.SubItems.Add("");
                                }

                                // Alarm Type
                                alarm_type alarmType = Alarm.alarmDetect.instance.DetermineAlarmType(t, Convert.ToDouble(item.VAL1.Value), Convert.ToDouble(item.VAL1.Threshold),
                                    Convert.ToDouble(item.VAL1.Threshold_Two), Convert.ToDouble(item.VAL1.Threshold_Three));

                                // Set Row background color
                                switch (alarmType)
                                {
                                    case alarm_type.mini:
                                        i.BackColor = Color.LightGreen;
                                        break;

                                    case alarm_type.med:
                                        i.BackColor = Color.Orange;
                                        break;

                                    case alarm_type.high:
                                        i.BackColor = Color.Red;
                                        break;

                                    default:
                                        break;
                                }

                                i.SubItems.Add(item.VAL1.Threshold.ToString());
                                i.SubItems.Add(item.VAL1.Threshold_Two.ToString());
                                i.SubItems.Add(item.VAL1.Threshold_Three.ToString());
                                i.SubItems.Add(item.VAL1.Value.ToString());
                                i.SubItems.Add(item.VAL1.InUse.ToString());


                                this.listView1.Items.Add(i);
                                progressBar1.PerformStep();
                            }


                        }
                        catch (Exception Ex)
                        {
                            MessageBox.Show(Ex.Message, "错误！", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        }
                        break;

                    case sensor_type.humidity:
                        try
                        {
                            Monitor_DB.instance.db.Refresh(System.Data.Linq.RefreshMode.KeepChanges, Monitor_DB.instance.db.tb_humidity);
                            s_type = sensor_type.humidity;

                            var queryTemp =
                                from tem in Monitor_DB.instance.db.tb_humidity
                                join mon in Monitor_DB.instance.db.tb_monitor_device on tem.MonitorDevice_Id equals mon.ID
                                select new { VAL1 = tem, VAL2 = mon };

                            this.listView1.Columns.Clear();

                            this.listView1.Columns.Add("监控箱设备号");
                            this.listView1.Columns[0].Width = 160;
                            this.listView1.Columns.Add("传感器序列号");
                            this.listView1.Columns[1].Width = 100;
                            this.listView1.Columns.Add("名称");
                            this.listView1.Columns[2].Width = 130;
                            this.listView1.Columns.Add("区域");
                            this.listView1.Columns.Add("状态");
                            this.listView1.Columns.Add("阀值1");
                            this.listView1.Columns.Add("阀值2");
                            this.listView1.Columns.Add("阀值3");
                            this.listView1.Columns.Add("湿度");
                            this.listView1.Columns.Add("在用");
                            this.listView1.Tag = Monitor_DB.instance.db.tb_humidity;

                            this.数据上报ToolStripMenuItem.Enabled = true;
                            this.智能门锁ToolStripMenuItem2.Enabled = false;

                            progressBar1.Visible = true;
                            progressBar1.Value = 0;
                            progressBar1.Minimum = 0;
                            progressBar1.Maximum = queryTemp.Count();
                            progressBar1.Step = 1;
                            foreach (var item in queryTemp)
                            {


                                ListViewItem i = new ListViewItem(item.VAL2.ID.ToString());
                                i.SubItems[0].Name = item.VAL2.ID.ToString();
                                i.SubItems[0].Text = item.VAL2.Name.ToString();

                                i.ImageIndex = 4;
                                i.SubItems.Add(item.VAL1.ID.ToString());
                                i.SubItems.Add(item.VAL1.Name);
                                i.SubItems.Add(item.VAL2.Zone);

                                if (item.VAL1.Status != null)
                                {
                                    i.SubItems.Add(item.VAL1.Status.ToString());
                                }
                                else
                                {
                                    i.SubItems.Add("");
                                }

                                // Alarm Type
                                alarm_type alarmType = Alarm.alarmDetect.instance.DetermineAlarmType(t, Convert.ToDouble(item.VAL1.Value), Convert.ToDouble(item.VAL1.Threshold),
                                    Convert.ToDouble(item.VAL1.Threshold_Two), Convert.ToDouble(item.VAL1.Threshold_Three));

                                // Set Row background color
                                switch (alarmType)
                                {
                                    case alarm_type.mini:
                                        i.BackColor = Color.LightGreen;
                                        break;

                                    case alarm_type.med:
                                        i.BackColor = Color.Orange;
                                        break;

                                    case alarm_type.high:
                                        i.BackColor = Color.Red;
                                        break;

                                    default:
                                        break;
                                }

                                i.SubItems.Add(item.VAL1.Threshold.ToString());
                                i.SubItems.Add(item.VAL1.Threshold_Two.ToString());
                                i.SubItems.Add(item.VAL1.Threshold_Three.ToString());
                                i.SubItems.Add(item.VAL1.Value.ToString());
                                i.SubItems.Add(item.VAL1.InUse.ToString());

                                this.listView1.Items.Add(i);
                                progressBar1.PerformStep();

                            }
                        }
                        catch (Exception Ex)
                        {
                            MessageBox.Show(Ex.Message, "错误！", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        }
                        break;

                    case sensor_type.voltage:
                        try
                        {
                            Monitor_DB.instance.db.Refresh(System.Data.Linq.RefreshMode.KeepChanges, Monitor_DB.instance.db.tb_voltage);

                            s_type = sensor_type.voltage;

                            var queryTemp =
                                from tem in Monitor_DB.instance.db.tb_voltage
                                join mon in Monitor_DB.instance.db.tb_monitor_device on tem.MonitorDevice_Id equals mon.ID
                                select new { VAL1 = tem, VAL2 = mon };

                            this.listView1.Columns.Clear();

                            this.listView1.Columns.Add("监控箱设备号");
                            this.listView1.Columns[0].Width = 160;
                            this.listView1.Columns.Add("传感器序列号");
                            this.listView1.Columns[1].Width = 100;
                            this.listView1.Columns.Add("名称");
                            this.listView1.Columns[2].Width = 130;
                            this.listView1.Columns.Add("区域");
                            this.listView1.Columns.Add("状态");
                            this.listView1.Columns.Add("阀值1");
                            this.listView1.Columns.Add("阀值2");
                            this.listView1.Columns.Add("阀值3");
                            this.listView1.Columns.Add("电压");
                            this.listView1.Columns.Add("在用");
                            this.listView1.Tag = Monitor_DB.instance.db.tb_voltage;

                            this.数据上报ToolStripMenuItem.Enabled = true;
                            this.智能门锁ToolStripMenuItem2.Enabled = false;

                            progressBar1.Visible = true;
                            progressBar1.Value = 0;
                            progressBar1.Minimum = 0;
                            progressBar1.Maximum = queryTemp.Count();
                            progressBar1.Step = 1;
                            foreach (var item in queryTemp)
                            {
                                ListViewItem i = new ListViewItem(item.VAL2.ID.ToString());
                                i.SubItems[0].Name = item.VAL2.ID.ToString();
                                i.SubItems[0].Text = item.VAL2.Name.ToString();

                                i.ImageIndex = 7;

                                i.SubItems.Add(item.VAL1.ID.ToString());
                                i.SubItems.Add(item.VAL1.Name);
                                i.SubItems.Add(item.VAL2.Zone);
                                if (item.VAL1.Status != null)
                                {
                                    i.SubItems.Add(item.VAL1.Status.ToString());
                                }
                                else
                                {
                                    i.SubItems.Add("");
                                }

                                // Alarm Type
                                alarm_type alarmType = Alarm.alarmDetect.instance.DetermineAlarmType(t, Convert.ToDouble(item.VAL1.Value), Convert.ToDouble(item.VAL1.Threshold),
                                    Convert.ToDouble(item.VAL1.Threshold_Two), Convert.ToDouble(item.VAL1.Threshold_Three));

                                // Set Row background color
                                switch (alarmType)
                                {
                                    case alarm_type.mini:
                                        i.BackColor = Color.LightGreen;
                                        break;

                                    case alarm_type.med:
                                        i.BackColor = Color.Orange;
                                        break;

                                    case alarm_type.high:
                                        i.BackColor = Color.Red;
                                        break;

                                    default:
                                        break;
                                }

                                i.SubItems.Add(item.VAL1.Threshold.ToString());
                                i.SubItems.Add(item.VAL1.Threshold_Two.ToString());
                                i.SubItems.Add(item.VAL1.Threshold_Three.ToString());
                                i.SubItems.Add(item.VAL1.Value.ToString());
                                i.SubItems.Add(item.VAL1.InUse.ToString());

                                this.listView1.Items.Add(i);
                                progressBar1.PerformStep();
                            }
                        }
                        catch (Exception Ex)
                        {
                            MessageBox.Show(Ex.Message, "错误！", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        }
                        break;

                    case sensor_type.door:
                        try
                        {
                            Monitor_DB.instance.db.Refresh(System.Data.Linq.RefreshMode.KeepChanges, Monitor_DB.instance.db.tb_door);

                            s_type = sensor_type.door;

                            var queryTemp =
                                from tem in Monitor_DB.instance.db.tb_door
                                join mon in Monitor_DB.instance.db.tb_monitor_device on tem.MonitorDevice_Id equals mon.ID
                                select new { VAL1 = tem, VAL2 = mon };

                            this.listView1.Columns.Clear();

                            this.listView1.Columns.Add("监控箱设备号");
                            this.listView1.Columns[0].Width = 160;
                            this.listView1.Columns.Add("传感器序列号");
                            this.listView1.Columns[1].Width = 100;
                            this.listView1.Columns.Add("名称");
                            this.listView1.Columns[2].Width = 130;
                            this.listView1.Columns.Add("区域");
                            this.listView1.Columns.Add("前/后门");
                            this.listView1.Columns.Add("状态");
                            this.listView1.Columns.Add("在用");
                            this.listView1.Tag = Monitor_DB.instance.db.tb_door;

                            this.数据上报ToolStripMenuItem.Enabled = false;
                            this.智能门锁ToolStripMenuItem2.Enabled = true;

                            progressBar1.Visible = true;
                            progressBar1.Value = 0;
                            progressBar1.Minimum = 0;
                            progressBar1.Maximum = queryTemp.Count();
                            progressBar1.Step = 1;
                            foreach (var item in queryTemp)
                            {
                                ListViewItem i = new ListViewItem(item.VAL2.ID.ToString());
                                i.SubItems[0].Name = item.VAL2.ID.ToString();
                                i.SubItems[0].Text = item.VAL2.Name.ToString();

                                i.ImageIndex = 3;

                                i.SubItems.Add(item.VAL1.ID.ToString());
                                i.SubItems.Add(item.VAL1.Name);
                                i.SubItems.Add(item.VAL2.Zone);
                                i.SubItems.Add(item.VAL1.Door_Type);

                                if (item.VAL1.Status == true)
                                {
                                    i.SubItems.Add("开");
                                }
                                else
                                {
                                    i.SubItems.Add("关");
                                }

                                i.SubItems.Add(item.VAL1.InUse.ToString());

                                this.listView1.Items.Add(i);
                                progressBar1.PerformStep();
                            }
                        }
                        catch (Exception Ex)
                        {
                            MessageBox.Show(Ex.Message, "错误！", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        }
                        break;

                    case sensor_type.shake:
                        try
                        {
                            Monitor_DB.instance.db.Refresh(System.Data.Linq.RefreshMode.KeepChanges, Monitor_DB.instance.db.tb_shake);

                            s_type = sensor_type.shake;

                            var queryTemp =
                                from tem in Monitor_DB.instance.db.tb_shake
                                join mon in Monitor_DB.instance.db.tb_monitor_device on tem.MonitorDevice_Id equals mon.ID
                                select new { VAL1 = tem, VAL2 = mon };

                            this.listView1.Columns.Clear();

                            this.listView1.Columns.Add("监控箱设备号");
                            this.listView1.Columns[0].Width = 160;
                            this.listView1.Columns.Add("传感器序列号");
                            this.listView1.Columns[1].Width = 100;
                            this.listView1.Columns.Add("名称");
                            this.listView1.Columns[2].Width = 130;
                            this.listView1.Columns.Add("区域");
                            this.listView1.Columns.Add("状态");
                            this.listView1.Columns.Add("阀值1");
                            this.listView1.Columns.Add("阀值2");
                            this.listView1.Columns.Add("阀值3");
                            this.listView1.Columns.Add("震动值");
                            this.listView1.Columns.Add("在用");
                            this.listView1.Tag = Monitor_DB.instance.db.tb_shake;

                            this.数据上报ToolStripMenuItem.Enabled = true;
                            this.智能门锁ToolStripMenuItem2.Enabled = false;

                            progressBar1.Visible = true;
                            progressBar1.Value = 0;
                            progressBar1.Minimum = 0;
                            progressBar1.Maximum = queryTemp.Count();
                            progressBar1.Step = 1;
                            foreach (var item in queryTemp)
                            {

                                ListViewItem i = new ListViewItem(item.VAL2.ID.ToString());
                                i.SubItems[0].Name = item.VAL2.ID.ToString();
                                i.SubItems[0].Text = item.VAL2.Name.ToString();

                                i.ImageIndex = 5;

                                i.SubItems.Add(item.VAL1.ID.ToString());
                                i.SubItems.Add(item.VAL1.Name);
                                i.SubItems.Add(item.VAL2.Zone);
                                if (item.VAL1.Status != null)
                                {
                                    i.SubItems.Add(item.VAL1.Status.ToString());
                                }
                                else
                                {
                                    i.SubItems.Add("");
                                }

                                // Alarm Type
                                alarm_type alarmType = Alarm.alarmDetect.instance.DetermineAlarmType(t, Convert.ToDouble(item.VAL1.Value), Convert.ToDouble(item.VAL1.Threshold),
                                    Convert.ToDouble(item.VAL1.Threshold_Two), Convert.ToDouble(item.VAL1.Threshold_Three));

                                // Set Row background color
                                switch (alarmType)
                                {
                                    case alarm_type.mini:
                                        i.BackColor = Color.LightGreen;
                                        break;

                                    case alarm_type.med:
                                        i.BackColor = Color.Orange;
                                        break;

                                    case alarm_type.high:
                                        i.BackColor = Color.Red;
                                        break;

                                    default:
                                        break;
                                }

                                i.SubItems.Add(item.VAL1.Threshold.ToString());
                                i.SubItems.Add(item.VAL1.Threshold_Two.ToString());
                                i.SubItems.Add(item.VAL1.Threshold_Three.ToString());
                                i.SubItems.Add(item.VAL1.Value.ToString());
                                i.SubItems.Add(item.VAL1.InUse.ToString());

                                this.listView1.Items.Add(i);
                                progressBar1.PerformStep();
                            }
                        }
                        catch (Exception Ex)
                        {
                            MessageBox.Show(Ex.Message, "错误！", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        }
                        break;

                    default:

                        break;

                }
            }
            catch (Exception Ex)
            {
                MessageBox.Show(Ex.Message, "错误！", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

            progressBar1.Visible = false;
        }
        private void listView2_Click(object sender, EventArgs e)
        {
            clickListView2();
        }

        private void listView2_DoubleClick(object sender, EventArgs e)
        { 
            
        }

        // ListView1 右键菜单
        private void contextMenuStrip_listView_Opening(object sender, CancelEventArgs e)
        {
            if (this.listView1.Tag != null)
            {
                if (this.listView1.Tag.GetType() == typeof(TreeNode))
                {
                    this.contextMenuStrip_listView.Enabled = false;
                }
                else
                {
                    this.contextMenuStrip_listView.Enabled = true;
                }
            }

        }
        private void 添加ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                if (this.listView1.Tag == null)
                {
                    return;
                }

                if (this.listView1.Tag.GetType() != typeof(TreeNode))
                {
                    ListViewItem addItem = null;
                    if (this.listView1.SelectedItems.Count != 0)
                    {
                        addItem = (ListViewItem)this.listView1.SelectedItems[0].Clone();
                        addItem.SubItems[0].Name = ((ListViewItem)this.listView1.SelectedItems[0]).SubItems[0].Name;
                    }

                    Form addForm = new Add_sensorForm(addItem, s_type, this, true);
                    addForm.Show();
                }
                else
                {
                    // Add MonitorDevice
                }
            }
            catch (Exception Ex)
            {
                MessageBox.Show(Ex.Message, "错误！", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void listView1_DoubleClick(object sender, EventArgs e)
        {
            try
            {
                if (this.listView1.Tag == Monitor_DB.instance.db.tb_door ||
                    this.listView1.Tag == Monitor_DB.instance.db.tb_tempture ||
                    this.listView1.Tag == Monitor_DB.instance.db.tb_humidity ||
                    this.listView1.Tag == Monitor_DB.instance.db.tb_shake ||
                    this.listView1.Tag == Monitor_DB.instance.db.tb_voltage 
                    )
                
                {


                    if (this.listView1.SelectedItems.Count != 0)
                    {
                        ListViewItem item = this.listView1.SelectedItems[0];

                        if (item.BackColor != Color.White)
                        {
                            double value = 0;
                            if (item.SubItems[8].Text != "")
                            {
                                value = Convert.ToDouble(item.SubItems[8].Text);
                            }

                            // 获取当前Alarm
                            IEnumerable<tb_alarm> qAlarm =
                            from alarm in Monitor_DB.instance.db.tb_alarm
                            where alarm.MonitorDevice_Id == Convert.ToInt32(item.SubItems[0].Name)
                            where alarm.Device_Id == Convert.ToInt32(item.SubItems[1].Text)
                            where alarm.Value == value
                            select alarm;

                            if (qAlarm.Count() > 0)
                            {
                                Alarm.FormAlarmDetect h = new Alarm.FormAlarmDetect(qAlarm.First());
                                h.Show();
                            }
                            else
                            {
                                MessageBox.Show("告警表没有记录！");
                                编辑ToolStripMenuItem_Click(sender, e);
                            }
                        }
                        else
                        {
                            编辑ToolStripMenuItem_Click(sender, e);
                        }
                    }

                }
            
            }
            catch (Exception Ex)
            {
                MessageBox.Show(Ex.Message, "错误！", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            
        }

        private void 编辑ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                if (this.listView1.Tag == null)
                {
                    return;
                }

                if (this.listView1.Tag.GetType() != typeof(TreeNode))
                {
                    if (this.listView1.SelectedItems.Count != 0)
                    {
                        Form editForm = new Add_sensorForm(this.listView1.SelectedItems[0], s_type, this, false);
                        editForm.Text = "编辑传感器";

                        editForm.Show();
                    }
                }
                else
                {
                    // Edit MonitorDevice
                }
                
            }
            catch (Exception Ex)
            {
                MessageBox.Show(Ex.Message, "错误！", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void deleteItem(ListViewItem delItem)
        {
            try
            {
                if (delItem != null)
                {
                    sensor_type t = (sensor_type)this.listView2.SelectedItems[0].Tag;
 
                    switch (t)
                    {
                        case sensor_type.door:

                            var result =
                                from del in Monitor_DB.instance.db.tb_door
                                where del.ID == Convert.ToInt32(delItem.SubItems[1].Text)
                                select del;

                            Monitor_DB.instance.db.tb_door.DeleteAllOnSubmit(result);
                            Monitor_DB.instance.db.SubmitChanges();

                            break;

                        case sensor_type.tempture:

                            var tem =
                                from del in Monitor_DB.instance.db.tb_tempture
                                where del.ID == Convert.ToInt32(delItem.SubItems[1].Text)
                                select del;

                            Monitor_DB.instance.db.tb_tempture.DeleteAllOnSubmit(tem);
                            Monitor_DB.instance.db.SubmitChanges();

                            break;

                        case sensor_type.humidity:

                            var hum =
                                from del in Monitor_DB.instance.db.tb_humidity
                                where del.ID == Convert.ToInt32(delItem.SubItems[1].Text)
                                select del;

                            Monitor_DB.instance.db.tb_humidity.DeleteAllOnSubmit(hum);
                            Monitor_DB.instance.db.SubmitChanges();

                            break;

                        case sensor_type.shake:

                            var shake =
                                from del in Monitor_DB.instance.db.tb_shake
                                where del.ID == Convert.ToInt32(delItem.SubItems[1].Text)
                                select del;

                            Monitor_DB.instance.db.tb_shake.DeleteAllOnSubmit(shake);
                            Monitor_DB.instance.db.SubmitChanges();

                            break;

                        case sensor_type.voltage:

                            var vol =
                                from del in Monitor_DB.instance.db.tb_voltage
                                where del.ID == Convert.ToInt32(delItem.SubItems[1].Text)
                                select del;

                            Monitor_DB.instance.db.tb_voltage.DeleteAllOnSubmit(vol);
                            Monitor_DB.instance.db.SubmitChanges();

                            break;


                        default:
                            break;
                    }

                    MessageBox.Show("删除成功！", "", MessageBoxButtons.OK, MessageBoxIcon.Information);

                    this.listView1.Items.Remove(delItem);

                }
            }
            catch (Exception Ex)
            {
                MessageBox.Show(Ex.Message, "错误！", MessageBoxButtons.OK, MessageBoxIcon.Error);
                Monitor_DB.instance.Refresh_DB();
            }
        }
        private void 删除ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                if (this.listView1.Tag == null)
                {
                    return;
                }

                if (this.listView1.Tag.GetType() != typeof(TreeNode))
                {
                    if (this.listView1.SelectedItems.Count != 0)
                    {
                        DialogResult r = MessageBox.Show("删除所选记录？", "删除", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
                        if (r == DialogResult.Yes)
                        {
                            deleteItem(this.listView1.SelectedItems[0]);
                        }               
                    }
                }
                else
                {
                    // Delete MonitorDevice
                }

            }
            catch (Exception Ex)
            {
                MessageBox.Show(Ex.Message, "错误！", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private string getMonitorDevId_Name(string MonitorDev_Id)
        {
            try
            {
                var queryNum =
                from num in Monitor_DB.instance.db.tb_monitor_device
                where num.ID == Convert.ToInt32(MonitorDev_Id)
                select num;

                return queryNum.First().Name;
            }
            catch (Exception Ex)
            {
                MessageBox.Show(Ex.Message, "错误！", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return null;
            }
        }

        private void 数据上报ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            
            try
            {
                if (this.listView1.Tag == null)
                {
                    return;
                }

                if (this.listView1.Tag.GetType() != typeof(TreeNode))
                {
                    if (this.listView1.SelectedItems.Count != 0)
                    {
                        // 交接箱序列号
                        string monitorDevId = this.listView1.SelectedItems[0].SubItems[0].Name;

                        // 设备号
                        string deviceId = this.listView1.SelectedItems[0].SubItems[2].Text;

                        // 传感器
                        string sensor = ""; 
                        if (this.listView2.SelectedItems.Count != 0)
                        {
                            sensor = this.listView2.SelectedItems[0].Text;
                        }

                        if (ConfigCmd.configCommand.instance.RequestDevData(deviceId, monitorDevId))
                        {
                            UserLog.adduseroperlog(User.UserID, User.Username, "数据上报", "成功", getMonitorDevId_Name(monitorDevId), deviceId, sensor);
                            MessageBox.Show("已进入发送队列！", "", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        }
                        else
                        {
                            UserLog.adduseroperlog(User.UserID, User.Username, "数据上报", "失败", getMonitorDevId_Name(monitorDevId), deviceId, sensor);
                            MessageBox.Show("发送失败!", "", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        }
                    }
                }
                else
                {
                    // Edit MonitorDevice
                }

            }
            catch (Exception Ex)
            {
                MessageBox.Show(Ex.Message, "错误！", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void 指定设备号ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                if (this.listView1.Tag == null)
                {
                    return;
                }

                if (this.listView1.Tag.GetType() != typeof(TreeNode))
                {
                    if (this.listView1.SelectedItems.Count != 0)
                    {
                        // 交接箱序列号
                        string monitorDevId = this.listView1.SelectedItems[0].SubItems[0].Name;

                        // 设备号
                        string deviceId = this.listView1.SelectedItems[0].SubItems[2].Text;

                        // 传感器
                        string sensor = "";
                        if (this.listView2.SelectedItems.Count != 0)
                        {
                            sensor = this.listView2.SelectedItems[0].Text;
                        }

                        if (ConfigCmd.configCommand.instance.SendDeviceId(deviceId, monitorDevId))
                        {
                            UserLog.adduseroperlog(User.UserID, User.Username, "指定设备号", "成功", getMonitorDevId_Name(monitorDevId), deviceId, sensor);
                            MessageBox.Show("已进入发送队列！", "", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        }
                        else
                        {
                            UserLog.adduseroperlog(User.UserID, User.Username, "指定设备号", "失败", getMonitorDevId_Name(monitorDevId), deviceId, sensor);
                            MessageBox.Show("发送失败!", "", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        }
                        
                    }
                }
                else
                {
                    // Edit MonitorDevice
                }

            }
            catch (Exception Ex)
            {
                MessageBox.Show(Ex.Message, "错误！", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void 复位设备ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                if (this.listView1.Tag == null)
                {
                    return;
                }

                if (this.listView1.Tag.GetType() != typeof(TreeNode))
                {
                    if (this.listView1.SelectedItems.Count != 0)
                    {
                        // 交接箱序列号
                        string monitorDevId = this.listView1.SelectedItems[0].SubItems[0].Name;

                        // 设备号
                        string deviceId = this.listView1.SelectedItems[0].SubItems[2].Text;

                        // 传感器
                        string sensor = "";
                        if (this.listView2.SelectedItems.Count != 0)
                        {
                            sensor = this.listView2.SelectedItems[0].Text;
                        }

                        if (ConfigCmd.configCommand.instance.ResetDevice(deviceId, monitorDevId))
                        {
                            UserLog.adduseroperlog(User.UserID, User.Username, "复位设备", "成功", getMonitorDevId_Name(monitorDevId), deviceId, sensor);
                            MessageBox.Show("已进入发送队列！", "", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        }
                        else
                        {
                            UserLog.adduseroperlog(User.UserID, User.Username, "复位设备", "失败", getMonitorDevId_Name(monitorDevId), deviceId, sensor);
                            MessageBox.Show("发送失败!", "", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        }
                        
                    }
                }
                else
                {
                    // Edit MonitorDevice
                }

            }
            catch (Exception Ex)
            {
                MessageBox.Show(Ex.Message, "错误！", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void 指定设备IP地址端口号ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                if (this.listView1.Tag == null)
                {
                    return;
                }

                if (this.listView1.Tag.GetType() != typeof(TreeNode))
                {
                    if (this.listView1.SelectedItems.Count != 0)
                    {
                        // 交接箱序列号
                        string monitorDevId = this.listView1.SelectedItems[0].SubItems[0].Name;

                        // 设备号
                        string deviceId = this.listView1.SelectedItems[0].SubItems[2].Text;

                        Form FormIP = new ConfigCmd.FormIPconfig(deviceId, monitorDevId, getMonitorDevId_Name(monitorDevId));
                        FormIP.Show();
                    }
                }
                else
                {
                    // Edit MonitorDevice
                }

            }
            catch (Exception Ex)
            {
                MessageBox.Show(Ex.Message, "错误！", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void 指定手机号码ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                if (this.listView1.Tag == null)
                {
                    return;
                }

                if (this.listView1.Tag.GetType() != typeof(TreeNode))
                {
                    
                    if (this.listView1.SelectedItems.Count != 0)
                    {
                        string monitorDevId = "";
                        string deviceId = "";

                        // 交接箱序列号
                        if (this.listView1.SelectedItems[0].SubItems[0] != null)
                        {
                            monitorDevId = this.listView1.SelectedItems[0].SubItems[0].Name;
                        }
                        // 设备号
                        if (this.listView1.SelectedItems[0].SubItems[2] != null)
                        {
                            deviceId = this.listView1.SelectedItems[0].SubItems[2].Text;
                        }

                        Form FormPhone = new ConfigCmd.FormPhoneConfig(deviceId, monitorDevId, getMonitorDevId_Name(monitorDevId));
                        FormPhone.Show();
                    }
                }
                else
                {
                    // Edit MonitorDevice
                }

            }
            catch (Exception Ex)
            {
                MessageBox.Show(Ex.Message, "错误！", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void 开门ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                if (this.listView1.Tag == null)
                {
                    return;
                }

                if (this.listView1.Tag.GetType() != typeof(TreeNode))
                {
                    if (this.listView1.SelectedItems.Count != 0)
                    {
                        // 交接箱序列号
                        string monitorDevId = this.listView1.SelectedItems[0].SubItems[0].Name;

                        // 设备序列号
                        string deviceSnId = this.listView1.SelectedItems[0].SubItems[1].Text;

                        // 设备号
                        string deviceId = this.listView1.SelectedItems[0].SubItems[2].Text;     

                        // 前后门
                        bool frontDoor = true;
                        if(this.listView1.SelectedItems[0].SubItems[4].Text.Equals("前"))
                        {
                            frontDoor = true;
                        }
                        else if(this.listView1.SelectedItems[0].SubItems[4].Text.Equals("后"))
                        {
                            frontDoor = false;
                        }

                        Form doorCtrlForm = new ConfigCmd.FormDoorCtrl(deviceId,
                                                                        deviceSnId,
                                                                        monitorDevId,
                                                                        getMonitorDevId_Name(monitorDevId),
                                                                        frontDoor);
                        doorCtrlForm.Show();
                    }
                }
                else
                {
                    // Edit MonitorDevice
                }

            }
            catch (Exception Ex)
            {
                MessageBox.Show(Ex.Message, "错误！", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void 门卡授权ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                if (this.listView1.Tag == null)
                {
                    return;
                }

                if (this.listView1.Tag.GetType() != typeof(TreeNode))
                {
                    if (this.listView1.SelectedItems.Count != 0)
                    {
                        // 交接箱序列号
                        string monitorDevId = this.listView1.SelectedItems[0].SubItems[0].Name;

                        // 设备序列号
                        string deviceSnId = this.listView1.SelectedItems[0].SubItems[1].Text;

                        // 设备号
                        string deviceId = this.listView1.SelectedItems[0].SubItems[2].Text;

                        Form doorCtrlForm = new CardConfig.FormCardAuthority(deviceId,
                                                                             deviceSnId,
                                                                             monitorDevId,
                                                                             getMonitorDevId_Name(monitorDevId),
                                                                             true);
                        doorCtrlForm.Show();
                    }
                }
                else
                {
                    // Edit MonitorDevice
                }

            }
            catch (Exception Ex)
            {
                MessageBox.Show(Ex.Message, "错误！", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void 门卡查询ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                if (this.listView1.Tag == null)
                {
                    return;
                }

                if (this.listView1.Tag.GetType() != typeof(TreeNode))
                {
                    if (this.listView1.SelectedItems.Count != 0)
                    {
                        // 交接箱序列号
                        string monitorDevId = this.listView1.SelectedItems[0].SubItems[0].Name;

                        // 设备序列号
                        string deviceSnId = this.listView1.SelectedItems[0].SubItems[1].Text;

                        // 设备号
                        string deviceId = this.listView1.SelectedItems[0].SubItems[2].Text;

                        Form doorCtrlForm = new CardConfig.FormCardAuthority(deviceId,
                                                                             deviceSnId,
                                                                             monitorDevId,
                                                                             getMonitorDevId_Name(monitorDevId),
                                                                             false);
                        doorCtrlForm.Show();
                    }
                }
                else
                {
                    // Edit MonitorDevice
                }

            }
            catch (Exception Ex)
            {
                MessageBox.Show(Ex.Message, "错误！", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void 门卡删除ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                if (this.listView1.Tag == null)
                {
                    return;
                }

                if (this.listView1.Tag.GetType() != typeof(TreeNode))
                {
                    if (this.listView1.SelectedItems.Count != 0)
                    {
                        // 交接箱序列号
                        string monitorDevId = this.listView1.SelectedItems[0].SubItems[0].Name;

                        // 设备号
                        string deviceId = this.listView1.SelectedItems[0].SubItems[2].Text;

                        Form delDoorForm = new ConfigCmd.FormDelDoor_AuthCard(deviceId,
                                                                              monitorDevId,
                                                                              getMonitorDevId_Name(monitorDevId));
                        delDoorForm.Show();

                    }
                }
                else
                {
                    // Edit MonitorDevice
                }

            }
            catch (Exception Ex)
            {
                MessageBox.Show(Ex.Message, "错误！", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        // 传感器编辑表
        private void 温度ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                Form FormTemp = new SensorTable.FormTemp();
                FormTemp.Show();
            }
            catch (Exception Ex)
            {
                MessageBox.Show(Ex.Message, "错误！", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void 湿度ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                Form FormHumi = new SensorTable.FormHumidity();
                FormHumi.Show();
            }
            catch (Exception Ex)
            {
                MessageBox.Show(Ex.Message, "错误！", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void 震动传感ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                Form FormShak = new SensorTable.FormShake();
                FormShak.Show();
            }
            catch (Exception Ex)
            {
                MessageBox.Show(Ex.Message, "错误！", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void 电压ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                Form FormVolt = new SensorTable.FormVoltage();
                FormVolt.Show();
            }
            catch (Exception Ex)
            {
                MessageBox.Show(Ex.Message, "错误！", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void 智能门锁ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                Form FormDoo = new SensorTable.FormDoor();
                FormDoo.Show();
            }
            catch (Exception Ex)
            {
                MessageBox.Show(Ex.Message, "错误！", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        

        // 配置编辑表
        private void 告警ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                Form FormAlarm = new SensorTable.FormAlarm();
                FormAlarm.Show();
            }
            catch (Exception Ex)
            {
                MessageBox.Show(Ex.Message, "错误！", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void 卡号ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                Form FormCard = new CardConfig.FormCard();
                FormCard.Show();
            }
            catch (Exception Ex)
            {
                MessageBox.Show(Ex.Message, "错误！", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void 监控箱ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                Form FormMon = new SensorTable.FormMonitorDev();
                FormMon.Show();
            }
            catch (Exception Ex)
            {
                MessageBox.Show(Ex.Message, "错误！", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void 电话号码ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                Form FormMobile = new SensorTable.FormMobile();
                FormMobile.Show();
            }
            catch (Exception Ex)
            {
                MessageBox.Show(Ex.Message, "错误！", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void 门卡权限ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                Form FormCardDoor = new SensorTable.FormCard_Door();
                FormCardDoor.Show();
            }
            catch (Exception Ex)
            {
                MessageBox.Show(Ex.Message, "错误！", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void 信息发送ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            
            try
            {
                Form FormConfigMsg = new ConfigMsg.FormConfigMsg();
                FormConfigMsg.Show();
            }
            catch (Exception Ex)
            {
                MessageBox.Show(Ex.Message, "错误！", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void 退出系统ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        // 监控箱，门卡，传感器图表
        private void 监控箱ToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            try
            {
                Form FormGraphicMon = new CrystalReport.FormGraphicMonDev();
                FormGraphicMon.Show();
            }
            catch (Exception Ex)
            {
                MessageBox.Show(Ex.Message, "错误！", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void 门卡ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                Form FormGraphicCard = new CrystalReport.FormGraphicCard();
                FormGraphicCard.Show();
            }
            catch (Exception Ex)
            {
                MessageBox.Show(Ex.Message, "错误！", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void 温度ToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            try
            {
                Form FormGraphicTemp = new CrystalReport.FormGraphicTemp();
                FormGraphicTemp.Show();
            }
            catch (Exception Ex)
            {
                MessageBox.Show(Ex.Message, "错误！", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void 湿度ToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            try
            {
                Form FormGraphicHum = new CrystalReport.FormGraphicHumidity();
                FormGraphicHum.Show();
            }
            catch (Exception Ex)
            {
                MessageBox.Show(Ex.Message, "错误！", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void 震动ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                Form FormGraphicSha = new CrystalReport.FormGraphicShake();
                FormGraphicSha.Show();
            }
            catch (Exception Ex)
            {
                MessageBox.Show(Ex.Message, "错误！", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void 电压ToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            try
            {
                Form FormGraphicVol = new CrystalReport.FormGraphicVoltage();
                FormGraphicVol.Show();
            }
            catch (Exception Ex)
            {
                MessageBox.Show(Ex.Message, "错误！", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void 智能门锁ToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            try
            {
                Form FormGraphicDoo = new CrystalReport.FormGraphicDoor();
                FormGraphicDoo.Show();
            }
            catch (Exception Ex)
            {
                MessageBox.Show(Ex.Message, "错误！", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void MonitorForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            try
            {
                DialogResult r = MessageBox.Show("确定退出监控系统？", "关闭程序", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
                if (r == DialogResult.OK)
                {
                    Alarm.alarmDetect.instance.Release_thread();
                }
                else if (r == DialogResult.Cancel)
                {
                    e.Cancel = true;

                }
            }
            catch (Exception Ex)
            {
                MessageBox.Show(Ex.Message, "错误！", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void MonitorForm_FormClosed(object sender, FormClosedEventArgs e)
        {
            UserLog.addloginrecord(User.UserID, User.Username, User.Userrole, "退出", User.IPAddress);

            if (this.loginwindow != null)
            {
                this.loginwindow.Close();
            }
        }

        private void toolStripButton10_Click(object sender, EventArgs e)
        {
            try
            {
                Alarm.alarmDetect.instance.detect_alarm();
                this.toolStripButton10.Enabled = false;
                this.toolStripButton11.Enabled = true;
            }
            catch (Exception Ex)
            {
                MessageBox.Show(Ex.Message, "错误！", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void toolStripButton11_Click(object sender, EventArgs e)
        {
            try
            {
                Alarm.alarmDetect.instance.stopAlarmSound();
                Alarm.alarmDetect.instance.Release_thread();

                this.toolStripButton10.Enabled = true;
                this.toolStripButton11.Enabled = false;
            }
            catch (Exception Ex)
            {
                MessageBox.Show(Ex.Message, "错误！", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void 增加用户ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                UserAddForm f = new UserAddForm();
                f.Show();
            }
            catch (Exception Ex)
            {
                MessageBox.Show(Ex.Message, "错误！", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void 修改用户ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                if (User.Userrole == "系统管理员")
                {
                    UserMofForm f = new UserMofForm();
                    f.Show();
                }
                else
                {
                    UserMofForm2 f = new UserMofForm2();
                    f.Show();
                }

            }
            catch (Exception Ex)
            {
                MessageBox.Show(Ex.Message, "错误！", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void 删除用户ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                UserDelForm f = new UserDelForm();
                f.Show();
            }
            catch (Exception Ex)
            {
                MessageBox.Show(Ex.Message, "错误！", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void 查询用户ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                UserSearchForm f = new UserSearchForm();
                f.Show();
            }
            catch (Exception Ex)
            {
                MessageBox.Show(Ex.Message, "错误！", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void 用户登陆日志ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                UserLoginLogForm userloginlog = new UserLoginLogForm();
                userloginlog.Show();
            }
            catch (Exception Ex)
            {
                MessageBox.Show(Ex.Message, "错误！", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void 用户操作日志ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                UserOperLogForm f = new UserOperLogForm();
                f.Show();
            }
            catch (Exception Ex)
            {
                MessageBox.Show(Ex.Message, "错误！", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void 用户管理日志ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                UserManageLogForm f = new UserManageLogForm();
                f.Show();
            }
            catch (Exception Ex)
            {
                MessageBox.Show(Ex.Message, "错误！", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void 用户授权日志ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                if (User.Userrole != "系统管理员")
                {
                    MessageBox.Show("请用系统管理员账号登陆！", "错误！", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }

                UserAuthLogForm f = new UserAuthLogForm();
                f.Show();
            }
            catch (Exception Ex)
            {
                MessageBox.Show(Ex.Message, "错误！", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void 授权用户ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                if (User.Userrole != "系统管理员")
                {
                    MessageBox.Show("请用系统管理员账号登陆！", "错误！", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }

                UserAuthForm f = new UserAuthForm();
                f.Show();
            }
            catch (Exception Ex)
            {
                MessageBox.Show(Ex.Message, "错误！", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void 用户配置日志ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                UserConfigLogForm f = new UserConfigLogForm();
                f.Show();
            }
            catch (Exception Ex)
            {
                MessageBox.Show(Ex.Message, "错误！", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        // 图表快捷图标
        private void toolStripButton_door_Click(object sender, EventArgs e)
        {
            智能门锁ToolStripMenuItem1_Click(sender, e);
        }

        private void toolStripButton_temp_Click(object sender, EventArgs e)
        {
            温度ToolStripMenuItem1_Click(sender, e);
        }

        private void toolStripButton1_humm_Click(object sender, EventArgs e)
        {
            湿度ToolStripMenuItem1_Click(sender, e);
        }

        private void toolStripButton1_shake_Click(object sender, EventArgs e)
        {
            震动ToolStripMenuItem_Click(sender, e);
        }

        private void toolStripButton1_voltage_Click(object sender, EventArgs e)
        {
            电压ToolStripMenuItem1_Click(sender, e);
        }

        private void toolStripButton1_send_Click(object sender, EventArgs e)
        {
            信息发送ToolStripMenuItem_Click(sender, e);
        }

        private void toolStripButton1_alarm_Click(object sender, EventArgs e)
        {
            告警ToolStripMenuItem_Click(sender, e);
        }

        private void toolStripButton5_Click_1(object sender, EventArgs e)
        {
            监控箱ToolStripMenuItem1_Click(sender, e);
        }

        // 帮助菜单
        private void 版本信息ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                ProductInfo f = new ProductInfo();
                f.Show();
            }
            catch (Exception Ex)
            {
                MessageBox.Show(Ex.Message, "错误！", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            
        }

        private void disablemenuitem()
        {
            if(User.Userrole != "系统管理员")
            {
                this.增加用户ToolStripMenuItem.Enabled = false;
                this.删除用户ToolStripMenuItem.Enabled = false;
                this.查询用户ToolStripMenuItem.Enabled = false;
                this.授权用户ToolStripMenuItem.Enabled = false;
                this.用户登陆日志ToolStripMenuItem.Enabled = false;
                this.用户操作日志ToolStripMenuItem.Enabled = false;
                this.用户管理日志ToolStripMenuItem.Enabled = false;
                this.用户授权日志ToolStripMenuItem.Enabled = false;
                this.用户配置日志ToolStripMenuItem.Enabled = false;
            

                this.数据上报ToolStripMenuItem.Enabled = (User.auth[0] == 1) ? true : false;
                this.指定设备号ToolStripMenuItem.Enabled = (User.auth[1] == 1) ? true : false;
                this.复位设备ToolStripMenuItem.Enabled = (User.auth[2] == 1) ? true : false;
                this.指定设备IP地址端口号ToolStripMenuItem.Enabled = (User.auth[3] == 1) ? true : false;
                this.智能门锁ToolStripMenuItem2.Enabled = (User.auth[4] == 1) ? true : false;
            }
        }

        private void 关于ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                Form info = new Help.FormVersionInfo();
                info.Show();
            }
            catch (Exception Ex)
            {
                MessageBox.Show(Ex.Message, "错误！", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void listView1_SizeChanged(object sender, EventArgs e)
        {
         //   BackImage();
        }

        public void BackImage()
        {
           // MessageBox.Show(Directory.GetCurrentDirectory());
            Bitmap myBitmap = new Bitmap(@"..\..\Resources\mainbg.jpg");
            this.listView1.BackgroundImage = ResizeImage(myBitmap, this.listView1.Width, this.listView1.Height);
        }


        public static Bitmap ResizeImage(Bitmap bmp, int newW, int newH)
        {
            try
            {
                Bitmap b = new Bitmap(newW, newH);
                Graphics g = Graphics.FromImage(b);
                g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;

                g.DrawImage(bmp, new Rectangle(0, 0, newW, newH), new Rectangle(0, 0, bmp.Width, bmp.Height), GraphicsUnit.Pixel);
                g.Dispose();

                return b;
            }
            catch
            {
                return null;
            }
        }

        private void 历史告警ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                Form f = new SensorTable.FormHistoryAlarm();
                f.Show();
            }
            catch (Exception Ex)
            {
                MessageBox.Show(Ex.Message, "错误！", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void treeView1_AfterLabelEdit(object sender, NodeLabelEditEventArgs e)
        {
            SqlConnection conn = new SqlConnection("integrated security=true; database=MONITOR_DB; server=localhost; connect timeout=30");
            if (e.Label != null && e.Label != "")
            {
                //MessageBox.Show();
                conn.Open();
                string RefreshString = "update tb_monitor_device set Zone = '"+e.Label+"' where ID = "+(e.Node.Index+1).ToString();
                //string RefreshString = "update tb_monitor_device set Zone = '" + e.Label + "' where Zone = " + treeView1.SelectedNode.Text;
                //MessageBox.Show(RefreshString);
                SqlCommand cmd = new SqlCommand(RefreshString,conn);
                cmd.ExecuteNonQuery();
                conn.Close();
                MessageBox.Show("修改成功！","提示信息",MessageBoxButtons.OK,MessageBoxIcon.Asterisk);
            }

        }

        private void 增加ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            panel1.Show();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            panel1.Visible = false;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            TreeNode newNode = new TreeNode(textBox1.Text);
            treeView1.SelectedNode.Nodes.Add(newNode);
            SqlConnection conn = new SqlConnection("integrated security=true; database=MONITOR_DB; server=localhost; connect timeout=30");
            conn.Open();
            string RefreshString = "insert into tb_monitor_device (Zone) values ('"+textBox1.Text+"')";
            SqlCommand cmd = new SqlCommand(RefreshString, conn);
            cmd.ExecuteNonQuery();
            conn.Close();
            MessageBox.Show("添加成功！", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
            textBox1.Text = "";
        }

        private void 删除ToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            DialogResult r = MessageBox.Show("确定删除？", "警告", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);

            try
            {
                if (r == DialogResult.OK)
                {
                    
                    SqlConnection conn = new SqlConnection("integrated security=true; database=MONITOR_DB; server=localhost; connect timeout=30");
                    conn.Open();
                    //string RefreshString = "delete from tb_monitor_device where ID = " + (treeView1.SelectedNode.Index + 1).ToString();
                    string RefreshString = "delete from tb_monitor_device where Zone = '"+treeView1.SelectedNode.Text +"'";
                    //MessageBox.Show(RefreshString);
                    SqlCommand cmd = new SqlCommand(RefreshString, conn);
                    cmd.ExecuteNonQuery();
                    conn.Close();
                    MessageBox.Show("删除成功！", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                    treeView1.Nodes.Remove(treeView1.SelectedNode);
                }
                else if (r == DialogResult.Cancel)
                {

                }
            }
            catch(Exception ex)
            {
                //MessageBox.Show(ex.Message);
                MessageBox.Show("区域中存在电压计，温度，湿度或者门，请先删除相应的部件！","警告",MessageBoxButtons.OK,MessageBoxIcon.Warning);
            }
        }

       

       


            
        }
    
}
